package cn.angis.common.config;

import cn.angis.common.codec.SerializedObjectCodec;
import cn.angis.common.constant.Crypto;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import com.power4j.kit.seq.core.Sequence;
import com.power4j.kit.seq.persistent.Partitions;
import com.power4j.kit.seq.persistent.SeqHolder;
import com.power4j.kit.seq.persistent.provider.SimpleLettuceSynchronizer;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.codec.CompressionCodec;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;

import java.util.Properties;


@Configuration
public class ConfigCommon {

    @Bean
    public SM2 sm2Init() {
        return SmUtil.sm2(Crypto.PRIVATEKEY, Crypto.PUBLICKEY);
    }

    @Bean
    public StatefulRedisConnection<String, Object> redisGzipInit(@Inject("${redis}") Properties props){
        RedisClient client = RedisClient.create("redis://"+props.getProperty("server"));
        return client.connect(CompressionCodec.valueCompressor(new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP));
    }

    @Bean
    public RedisClient redisInit(@Inject("${redis}") Properties props){
        return RedisClient.create("redis://"+props.getProperty("server"));
    }

    @Bean
    public Sequence<Long> sequence(@Inject("${redis}") Properties props) {
        RedisClient redisClient = RedisClient.create("redis://"+props.getProperty("server"));
        SimpleLettuceSynchronizer simpleLettuceSynchronizer = new SimpleLettuceSynchronizer("adstar", redisClient);
        return SeqHolder.builder()
                .name("star")
                .synchronizer(simpleLettuceSynchronizer)
                .partitionFunc(Partitions.MONTHLY)
                .initValue(0000000000L)
                .poolSize(10)
                .build();

        // @formatter:on
    }

}




